﻿using System;
using Ease.EaseDialog.Runtime.Data;
using UnityEngine;
using UnityEngine.UI;

namespace Ease.EaseDialog.Runtime.UI
{
    public class UIDialog : MonoBehaviour
    {
        public static UIDialog Instance;

        public Text textName;
        public Text textContent;
        public Button btnOk;
        public Action OverCallback = null;

        private void Awake()
        {
            Instance = this;
            btnOk.onClick.AddListener(OnOver);
            gameObject.SetActive(false);
        }

        private void OnDestroy()
        {
            Instance = null;
            btnOk.onClick.RemoveListener(OnOver);
        }

        public void SetData(BaseDialogData data, Action overCallBack)
        {
            gameObject.SetActive(true);
            textName.text = data.name;
            textContent.text = data.content;
            OverCallback = null;
            OverCallback = overCallBack;
        }

        private void OnOver()
        {
            gameObject.SetActive(false);
            OverCallback?.Invoke();
            OverCallback = null;
        }
    }
}